﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Data.Common.QueryBuilder
{
    /// <summary>
    /// Manage Where operations.
    /// </summary>
    public class DbQueryWhere
    {
        public DbQuerySelect EndWhere;

        public DbQueryWhere Or
        {
            get
            {
                this.ComparerUsed = true;
                this._Where.Add("OR");
                return this;
            }
        }

        public DbQueryWhere And
        {
            get
            {
                this.ComparerUsed = true;
                this._Where.Add("AND");
                return this;
            }
        }

        public String WhereString
        {
            get
            {
                return String.Join(" ", this._Where);
            }
        }
        private List<String> _Where;
        private Boolean ComparerUsed;

        public DbQueryWhere(DbQuerySelect parent)
        {
            this.EndWhere = parent;
            this.ComparerUsed = true;
            this._Where = new List<String>();
        }

        public DbQueryWhere QueryCompare(String comp)
        {
            if (this.ComparerUsed == false)
                throw new ArgumentException("Cannot genereate concationation, if operators 'and' or 'or' are not in use");
            this._Where.Add(comp);
            this.ComparerUsed = false;
            return this;
        }

        public override string ToString()
        {
            return String.Concat("WHERE ", this.WhereString);
        }
    }
}
